package com.yuejian_06.yuejian_06.model;

import com.yuejian_06.yuejian_06.Listener.MeListener;
import com.yuejian_06.yuejian_06.bean.ActivityBean;
import com.yuejian_06.yuejian_06.bean.ContestBean;
import com.yuejian_06.yuejian_06.bean.PostListBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.fragment.Constants;
import com.yuejian_06.yuejian_06.service.HomeService;
import com.yuejian_06.yuejian_06.service.MeService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by __ZL__ on 2017/10/13.
 */

public class HomeModel {

    private HomeService homeService;
    private Retrofit retrofit;
    public HomeModel(){
        retrofit = new Retrofit.Builder()
                .baseUrl(Constants.SERVERADDRESS)//增加返回值为Gson的支持（一实体类返回）
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void getActivityList(final MeListener meListener) {
        homeService=retrofit.create(HomeService.class);
        Call<List<ActivityBean>> call=homeService.getActivityList();
        call.enqueue(new Callback<List<ActivityBean>>() {
            @Override
            public void onResponse(Call<List<ActivityBean>> call, Response<List<ActivityBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<ActivityBean>> call, Throwable t) {
                meListener.onFail("活动ListModel失败!");
            }
        });
    }

    public void getContestList(final MeListener meListener) {
        homeService=retrofit.create(HomeService.class);
        Call<List<ContestBean>> call=homeService.getContestList();
        call.enqueue(new Callback<List<ContestBean>>() {
            @Override
            public void onResponse(Call<List<ContestBean>> call, Response<List<ContestBean>> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<ContestBean>> call, Throwable t) {
                meListener.onFail("评论ListModel失败!");
            }
        });
    }

    public void getActivityInfo(String activity_id,final MeListener meListener) {
        homeService=retrofit.create(HomeService.class);
        Call<ActivityBean> call=homeService.getActivityInfo(activity_id);
        call.enqueue(new Callback<ActivityBean>() {
            @Override
            public void onResponse(Call<ActivityBean> call, Response<ActivityBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<ActivityBean> call, Throwable t) {
                meListener.onFail("活动详情Model失败!");
            }
        });
    }

    public void getContestInfo(String contest_id,final MeListener meListener) {
        homeService=retrofit.create(HomeService.class);
        Call<ContestBean> call=homeService.getContestInfo(contest_id);
        call.enqueue(new Callback<ContestBean>() {
            @Override
            public void onResponse(Call<ContestBean> call, Response<ContestBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<ContestBean> call, Throwable t) {
                meListener.onFail("比赛详情Model失败!");
            }
        });
    }

    //报名
    public void getAddActivity(String user_id,String activity_id,String activity_name,String IDcard,String sum_price,String phonenum, final MeListener meListener) {
        homeService=retrofit.create(HomeService.class);
        Call<SuccessBean> call=homeService.getAddActivity(user_id,activity_id,activity_name,IDcard,sum_price,phonenum);
        call.enqueue(new Callback<SuccessBean>() {
            @Override
            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                if (response.isSuccessful()&&response.body()!=null)
                {
                    meListener.onResponse(response.body());

                }
                else meListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<SuccessBean> call, Throwable t) {
                meListener.onFail("报名Model失败!");
            }
        });
    }
}
